package View.User;

import Service.ServiceImpl.UserServiceImpl;
import Service.UserService;
import View.Index.Index_admin;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class DeleteUser extends JFrame {

    public DeleteUser(User user){
        init();
        componentInit(user);
    }
    public void init(){
        this.setTitle("删除用户");
        this.setSize(528,470);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user) {
        //------------------JLabel--------------------
        JLabel label1 = new JLabel();
        label1.setText("请输入你要删除的用户的用户名:");
        label1.setBounds(13, 17, 186, 30);
        JLabel label2 = new JLabel();
        label2.setText("查询结果如下:");
        label2.setBounds(137, 72, 189, 56);
        JLabel label3 = new JLabel();
        label3.setText("用户名:");
        label3.setBounds(109, 140, 65, 30);
        JLabel label5 = new JLabel();
        label5.setText("用户类型:");
        label5.setBounds(110, 193, 65, 30);
        JLabel label6 = new JLabel();
        label6.setText("学号:");
        label6.setBounds(111, 253, 65, 30);
        JLabel label7 = new JLabel();
        label7.setText("id:");
        label7.setBounds(111, 300, 65, 30);

        //------------------JTextField--------------------
        JTextField field1 = new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(211, 17, 148, 30);
        JTextField field2 = new JTextField();
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(207, 143, 149, 30);
        String[] item = new String[3];
        item[0]="请选择用户类型";
        item[1]="user";
        item[2]="admin";
        JComboBox comboBox1= new JComboBox(item);
        comboBox1.setBounds(203,192,148,30);
        JTextField field4 = new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(203, 254, 152, 30);
        JTextField field5 = new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(203, 300, 152, 30);

        //------------------JButton--------------------
        JButton button1 = new JButton();
        button1.setText("删除");
        button1.setEnabled(false);
        button1.setBounds(115, 370, 95, 27);
        JButton button2 = new JButton();
        button2.setText("退出");
        button2.setBounds(285, 370, 95, 27);
        JButton button3 = new JButton();
        button3.setText("点击查询");
        button3.setBounds(371, 19, 95, 27);


        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(field1);
        this.add(field2);
        this.add(comboBox1);
        this.add(field4);
        this.add(field5);
        this.add(button1);
        this.add(button2);
        this.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame frame = new JFrame();
                String username = field1.getText();

                UserService service = new UserServiceImpl();
                service.deleteUser(username);

                JOptionPane.showMessageDialog(frame,"删除成功!");
                field2.setText("");

                field4.setText("");
                field5.setText("");
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame frame = new JFrame();
                String username = field1.getText();
                UserService service = new UserServiceImpl();
                List<User> list = service.findUserByUsername(username);
                User user = list.get(0);
                if(user == null){
                    JOptionPane.showMessageDialog(frame,"用户不存在");
                    field1.setText("");
                }else {
                    JOptionPane.showMessageDialog(frame,"用户信息如下,请检查后再删除");
                    field2.setText(user.getUsername());
                    if(user.getType().equals("user")){
                        comboBox1.setSelectedIndex(1);
                    }else if(user.getType().equals("admin")){
                        comboBox1.setSelectedIndex(2);
                    }
                    field4.setText(user.getStunum());
                    field5.setText(String.valueOf(user.getId()));
                    field5.setEnabled(false);
                    button1.setEnabled(true);
                }
            }
        });
    }
    }
